﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace TimKen.DataAccess
{
	/// <summary>
	/// 数据访问类ShipItem。
	/// </summary>
	public class ShipItemDAO
	{
        public ShipItemDAO()
		{}
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        /// 
        /*
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ItemNo", "ShipItem");
        }
        */
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ItemNo,string ShipNo)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
                    new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
            parameters[0].Value = ItemNo;
            parameters[1].Value = ShipNo;

            int result = DbHelperSQL.RunProcedure("UP_ShipItem_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.ShipItem model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Unit", SqlDbType.NVarChar,50),
					new SqlParameter("@Quantity", SqlDbType.Float,8),
					new SqlParameter("@RefSONo", SqlDbType.VarChar,50),
					new SqlParameter("@RefSOItemNo", SqlDbType.Int,4),
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
            parameters[0].Value = model.ItemNo;
            parameters[1].Value = model.ItemCode;
            parameters[2].Value = model.Unit;
            parameters[3].Value = model.Quantity;
            parameters[4].Value = model.RefSONo;
            parameters[5].Value = model.RefSOItemNo;
            parameters[6].Value = model.ShipNo;

            DbHelperSQL.RunProcedure("UP_ShipItem_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.ShipItem model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
					new SqlParameter("@ItemCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Unit", SqlDbType.NVarChar,50),
					new SqlParameter("@Quantity", SqlDbType.Float,8),
					new SqlParameter("@RefSONo", SqlDbType.VarChar,50),
					new SqlParameter("@RefSOItemNo", SqlDbType.Int,4),
					new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
            parameters[0].Value = model.ItemNo;
            parameters[1].Value = model.ItemCode;
            parameters[2].Value = model.Unit;
            parameters[3].Value = model.Quantity;
            parameters[4].Value = model.RefSONo;
            parameters[5].Value = model.RefSOItemNo;
            parameters[6].Value = model.ShipNo;

            DbHelperSQL.RunProcedure("UP_ShipItem_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ItemNo,string ShipNo)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
                    new SqlParameter("@ShipNo", SqlDbType.VarChar,50)};
            parameters[0].Value = ItemNo;
            parameters[1].Value = ShipNo;

            DbHelperSQL.RunProcedure("UP_ShipItem_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.ShipItem GetModel(int ItemNo,string ShipNo)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ItemNo", SqlDbType.Int,4),
                    new SqlParameter("@ShipNo", SqlDbType.VarChar,50)
                                        };
            parameters[0].Value = ItemNo;
            parameters[1].Value = ShipNo;

            TimKen.DataEntity.ShipItem model = new TimKen.DataEntity.ShipItem();
            DataSet ds = DbHelperSQL.RunProcedure("UP_ShipItem_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ItemNo"].ToString() != "")
                {
                    model.ItemNo = int.Parse(ds.Tables[0].Rows[0]["ItemNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ItemCode"].ToString() != "")
                {
                    model.ItemCode = (string)ds.Tables[0].Rows[0]["ItemCode"];
                }
                model.Unit = ds.Tables[0].Rows[0]["Unit"].ToString();
                if (ds.Tables[0].Rows[0]["Quantity"].ToString() != "")
                {
                    model.Quantity = decimal.Parse(ds.Tables[0].Rows[0]["Quantity"].ToString());
                }
                model.RefSONo = ds.Tables[0].Rows[0]["RefSONo"].ToString();
                if (ds.Tables[0].Rows[0]["RefSOItemNo"].ToString() != "")
                {
                    model.RefSOItemNo = int.Parse(ds.Tables[0].Rows[0]["RefSOItemNo"].ToString());
                }
                model.ShipNo = ds.Tables[0].Rows[0]["ShipNo"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ItemNo,ItemCode,Unit,Quantity,RefSONo,RefSOItemNo,ShipNo ");
            strSql.Append(" FROM ShipItem ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ShipItem";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 获得出货情况列表
        /// </summary>
        public DataSet GetOrderShipList(string OrderID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderID", SqlDbType.VarChar, 255)};
            parameters[0].Value = OrderID;
            return DbHelperSQL.RunProcedure("UP_Ship_GetOrderShip", parameters, "ds");
        }

        public DataSet GetOrderItemShipList(string OrderItemID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderItemID", SqlDbType.VarChar, 255)};
            parameters[0].Value = OrderItemID;
            return DbHelperSQL.RunProcedure("UP_Ship_GetOrderItemShip", parameters, "ds");
        }

        #endregion  成员方法
	}
}

